import request from '@/utils/request'
import { apiUri } from '@/config/config'
function formatRequestUri(name) {
  return 'file/' + name
}
export function getUpConfig(key, bizpk, tag) {
  return request({
    url: formatRequestUri('config'),
    method: 'post',
    data: {
      DirKey: key,
      Tag: tag,
      LinkBizPk: bizpk
    }
  })
}

export function getfileUpUri(config) {
  return apiUri['file'] + formatRequestUri('up/' + config)
}

export function deleteFile(fileId) {
  return request({
    url: formatRequestUri('delete'),
    method: 'get',
    params: {
      fileId
    }
  })
}

export function getFile(fileId) {
  return request({
    url: formatRequestUri('get'),
    method: 'get',
    params: {
      fileId
    }
  })
}

export function saveSort(sort) {
  return request({
    url: formatRequestUri('sort'),
    method: 'post',
    data: {
      Sort: sort
    }
  })
}

export function preUpFile(data, dirKey) {
  return request({
    url: formatRequestUri('preup/' + dirKey),
    method: 'post',
    data
  })
}

export function copy(data) {
  return request({
    url: formatRequestUri('copy'),
    method: 'post',
    data
  })
}

export function getUpState(taskId) {
  return request({
    url: formatRequestUri('block/up/state'),
    method: 'get',
    params: {
      taskId
    }
  })
}

export function blockUpFile(data) {
  return request({
    url: formatRequestUri('block/begin'),
    method: 'post',
    data
  })
}

export function getThumbnailUri(id) {
  return apiUri['file'] + formatRequestUri('video/thumbnail/' + id + '.png')
}

export function getUserThumbnailUri(id) {
  return apiUri['file'] + formatRequestUri('user/video/thumbnail/' + id + '.png')
}
export function upFile(url, file, progress) {
  const data = new FormData()
  data.append('file', file, file.name)
  return request({
    url: url,
    method: 'post',
    data,
    timeout: 120000,
    headers: { 'Content-Type': 'multipart/form-data' },
    onUploadProgress: e => {
      const val = Math.round((e.loaded / e.total) * 100)
      progress(val)
    }
  })
}
export function upFileStream(url, file, name, progress) {
  const data = new FormData()
  data.append('file', file, name)
  return request({
    url: url,
    method: 'post',
    data,
    headers: { 'Content-Type': 'multipart/form-data' },
    onUploadProgress: e => {
      if (progress) {
        const val = Math.round((e.loaded / e.total) * 100) / 100
        progress(val)
      }
    }
  })
}
export function upBlockFile(file, taskId, task, progress) {
  const chunk = file.slice(task.begin, task.end)
  const data = new FormData()
  data.append('file', chunk, file.name)
  let old = 0
  return request({
    url: formatRequestUri('block/up') + '?fileId=' + taskId + '&index=' + task.index,
    method: 'post',
    data,
    timeout: 60000,
    headers: { 'Content-Type': 'multipart/form-data' },
    onUploadProgress: e => {
      progress(e.loaded - old)
      old = e.loaded
    }
  })
}

export function gets(key, bizpk, tag) {
  return request({
    url: formatRequestUri('gets'),
    method: 'post',
    data: {
      DirKey: key,
      Tag: tag,
      LinkBizPk: bizpk
    }
  })
}
